<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 22:51
 */

namespace app\ops\controller;


class My extends Base

{

    function index(){

        $list = db('my')->order('id desc')->paginate(10);

        $this->assign('list', $list);

        return $this->fetch();

    }

    function edit($id){

        if($post = request()->post()){
            db('my')->update([
                'id'=> $post['id'],
                'img'=> $post['img'],
                'content'=> $post['content'],
                'wx_gzh'=> $post['wx_gzh'],
                'icon'=> $post['icon'],
            ]);
            return success([]);
        }else{

            $data = db('my')->where(['id'=> $id])->field("id, img, content, wx_gzh, icon")->find();
//            dump($data);
            $this->assign('data', $data);
            return $this->fetch();
        }

    }

    function upload()
    {
        $file = request()->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['size'=>2097152])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                $url = DS . 'uploads' . DS . $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                return fail();
            }
        } else {
            return fail();
        }

        return success(['url' => config('host') . $url]);

    }

}